package com.jobson.model.Transcation;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import com.jobson.model.Address;
import com.jobson.model.History;

/**
 * Backing bean for Address entities.
 * <p>
 * This class provides CRUD functionality for all Address entities. It focuses purely on Java EE 6 standards (e.g.
 * <tt>&#64;ConversationScoped</tt> for state management, <tt>PersistenceContext</tt> for persistence,
 * <tt>CriteriaBuilder</tt> for searches) rather than introducing a CRUD framework or custom base class.
 */

public class AddressT extends DBTranscation<Address> {

    public AddressT() {
        super(Address.class);
    }

    private static final long serialVersionUID = 11L;

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;

    @Override
    public Boolean addToHistory(Address aAddress) throws Exception {

        Boolean isUpdated = false;
        Address oldAddress = findById(aAddress.getId());

        List<String> changes = getDifference(oldAddress, aAddress, aAddress.getUser().getName());
        for (String changeText : changes) {
            new History(changeText, aAddress.getTableId(), new Long(aAddress.getId())).save();
            isUpdated = true;
        }

        return isUpdated;
    }

    @SuppressWarnings({ "rawtypes" })
    public List getlistLike(String aColumn, String aLike) {

        String table = "Address";
        List list = new ArrayList<String>();
        list = getLikeList(table, aColumn, aLike, aColumn);
        return list;

    }

}